package common.element.column_cast;

import common.element.StringColumn;
import common.util.Configuration;
import org.apache.commons.lang3.time.FastDateFormat;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * Created by ibm on 2018/8/17.
 */
class StringCast
{
    static String datetimeFormat = "yyyy-MM-dd HH:mm:ss";

    static String dateFormat = "yyyy-MM-dd";

    static String timeFormat = "HH:mm:ss";

    static List<String> extraFormats = Collections.emptyList();

    static String timeZone = "GMT+8";

    static FastDateFormat dateFormatter;

    static FastDateFormat timeFormatter;

    static FastDateFormat datetimeFormatter;

    static TimeZone timeZoner;

    static String encoding = "UTF-8";

    static void init(final Configuration configuration)
    {
        StringCast.datetimeFormat = configuration.getString(
                "common.column.datetimeFormat", StringCast.datetimeFormat);
        StringCast.dateFormat = configuration.getString(
                "common.column.dateFormat", StringCast.dateFormat);
        StringCast.timeFormat = configuration.getString(
                "common.column.timeFormat", StringCast.timeFormat);
        StringCast.extraFormats = configuration.getList(
                "common.column.extraFormats", Collections.<String>emptyList(), String.class);

        StringCast.timeZone = configuration.getString("common.column.timeZone",
                StringCast.timeZone);
        StringCast.timeZoner = TimeZone.getTimeZone(StringCast.timeZone);

        StringCast.datetimeFormatter = FastDateFormat.getInstance(
                StringCast.datetimeFormat, StringCast.timeZoner);
        StringCast.dateFormatter = FastDateFormat.getInstance(
                StringCast.dateFormat, StringCast.timeZoner);
        StringCast.timeFormatter = FastDateFormat.getInstance(
                StringCast.timeFormat, StringCast.timeZoner);

        StringCast.encoding = configuration.getString("common.column.encoding",
                StringCast.encoding);
    }

    static Date asDate(final StringColumn column) throws ParseException
    {
        if (null == column.asString())
        {
            return null;
        }

        try
        {
            return StringCast.datetimeFormatter.parse(column.asString());
        } catch (ParseException ignored)
        {
        }

        try
        {
            return StringCast.dateFormatter.parse(column.asString());
        } catch (ParseException ignored)
        {
        }

        ParseException e;
        try
        {
            return StringCast.timeFormatter.parse(column.asString());
        } catch (ParseException ignored)
        {
            e = ignored;
        }

        for (String format : StringCast.extraFormats)
        {
            try
            {
                return FastDateFormat.getInstance(format, StringCast.timeZoner).parse(column.asString());
            } catch (ParseException ignored)
            {
                e = ignored;
            }
        }
        throw e;
    }

    static byte[] asBytes(final StringColumn column)
            throws UnsupportedEncodingException
    {
        if (null == column.asString())
        {
            return null;
        }

        return column.asString().getBytes(StringCast.encoding);
    }
}
